package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {

    @Reference
    private CommentApi commentApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 点赞
     */
    public ResponseEntity likeComment(String publishId) {
        //1、创建Conment对象
        Comment comment = new Comment();
        //2、设置对象属性
        comment.setPublishId(new ObjectId(publishId));
        comment.setUserId(UserHolder.getUserId());
        comment.setCommentType(1); //评论类型，1-点赞，2-评论，3-喜欢
        comment.setPubType(1); //操作数据  1、动态，2：视频，3：评论
        //3、调用API保存点赞数据，返回值点赞的总数
        long total = commentApi.save(comment);
        //4、将点赞记录存入redis数据库
        redisTemplate.opsForValue().set("publish_like_comment_"+UserHolder.getUserId()+"_"+
                publishId, "1");
        //5、构造返回值
        return ResponseEntity.ok(total);
    }

    /**
     * 取消点赞
     */
    public ResponseEntity dislikeComment(String publishId) {
        //1、创建Conment对象
        Comment comment = new Comment();
        //2、设置删除属性
        comment.setPublishId(new ObjectId(publishId));
        comment.setUserId(UserHolder.getUserId());
        comment.setCommentType(1);
        //3、调用api删除
        long total = commentApi.remove(comment);
        //4、删除redis中数据
        redisTemplate.delete("publish_like_comment_"+UserHolder.getUserId()+"_"+publishId);
        //5、构造返回
        return ResponseEntity.ok(total);
    }

    //喜欢
    public ResponseEntity<Long> loveComment(String publishId) {
        //1、创建Conment对象
        Comment comment = new Comment();
        //2、设置对象属性
        comment.setPublishId(new ObjectId(publishId));
        comment.setUserId(UserHolder.getUserId());
        comment.setCommentType(3);
        comment.setPubType(1); //操作数据  1、动态，2：视频，3：评论
        //3、调用API保存点赞数据，返回值点赞的总数
        long total = commentApi.save(comment);
        //4、将点赞记录存入redis数据库
        redisTemplate.opsForValue().set("publish_love_comment_"+UserHolder.getUserId()+"_"+
                publishId, "1");
        //5、构造返回值
        return ResponseEntity.ok(total);
    }

    public ResponseEntity<Long> unLoveComment(String publishId) {
        //1、创建Conment对象
        Comment comment = new Comment();
        //2、设置删除属性
        comment.setPublishId(new ObjectId(publishId));
        comment.setUserId(UserHolder.getUserId());
        comment.setCommentType(3);
        //3、调用api删除
        long total = commentApi.remove(comment);
        //4、删除redis中数据
        redisTemplate.delete("publish_love_comment_"+UserHolder.getUserId()+"_"+publishId);
        //5、构造返回
        return ResponseEntity.ok(total);
    }

    /**
     * 分页查询评论列表
     */
    public ResponseEntity queryCommentsList(Integer page, Integer pagesize, String movementId) {
        //1、调用API查询： PageResult中封装的都是Comment对象
        PageResult result = commentApi.findAll(page, pagesize, movementId);
        //2、获取page对象中的数据列表  list<Comment>
        List<Comment> items = (List<Comment>) result.getItems();
        //3、一个Comment对象转化为一个VO对象
        List<CommentVo> list = new ArrayList<>();
        for (Comment comment : items) {
            CommentVo vo = new CommentVo();
            //4、查询发送评论用户详情
            BeanUtils.copyProperties(comment, vo);
            vo.setId(comment.getId().toString());
            vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy年MM月dd日 HH:mm"));
            UserInfo info = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(info, vo);
            vo.setHasLiked(0); //是否点赞
            list.add(vo);
        }
        //5、构造返回值
        result.setItems(list);
        return ResponseEntity.ok(result);
    }

    //保存评论
    public ResponseEntity saveComments(String publishId, String content) {
        //1、创建Comment对象，并赋值
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(2);
        comment.setPubType(1);
        comment.setContent(content);
        comment.setUserId(UserHolder.getUserId());
        //2、调用API保存
        commentApi.save(comment);
        //3、返回
        return ResponseEntity.ok(null);
    }
}
